package com.swn.common.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.swn.common.util.PageListData;

/**
 * 通用Action
 */
public class BaseController {

	private static Logger log = Logger.getLogger("BaseController.class");
	@SuppressWarnings("unused")
	private static final long serialVersionUID = 5374158890497984842L;


	// 操作标志
	protected String operFlag;
	// 显示标志
	protected String dispFlag;
	// 分页对象
	PageListData pageListData = new PageListData();
	private int pageSize = 10;// 每页的条数
	private int pages = 1;// 当前页数

	// 弹出提示对话框,提示信息
	protected String alertMessage = "";
	// 展现数据列表
	@SuppressWarnings("rawtypes")
	protected List dataList = new ArrayList();
	// 用于批量删除数据时设置checkbox
	@SuppressWarnings("rawtypes")
	protected List checkList;

	@SuppressWarnings("rawtypes")
	public List getCheckList() {
		return checkList;
	}

	@SuppressWarnings("rawtypes")
	public void setCheckList(List checkList) {
		this.checkList = checkList;
	}

	public String getOperFlag() {
		return operFlag;
	}

	public void setOperFlag(String operFlag) {
		this.operFlag = operFlag;
	}

	public String getDispFlag() {
		return dispFlag;
	}

	public void setDispFlag(String dispFlag) {
		this.dispFlag = dispFlag;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public PageListData getPageListData() {
		return pageListData;
	}

	public void setPageListData(PageListData pageListData) {
		this.pageListData = pageListData;
	}

	public void setAlertMessage(String message) {
		alertMessage = "<script language='javascript'>alert('" + message
				+ "');</script>";
	}

	/**
	 * 弹出信息后，关闭窗口（一般用于模式对话框）
	 * 
	 * @param message
	 */
	public void setAlertMessageAndClose(String message) {
		alertMessage = "<script language='javascript'>alert('" + message
				+ "');close();</script>";
	}

	public String getAlertMessage() {
		return alertMessage;
	}

	@SuppressWarnings("rawtypes")
	public List getDataList() {
		return dataList;
	}

	@SuppressWarnings("rawtypes")
	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
	
	public void setException(Logger logger,Exception e,Map<String, Object> result){
		logger.error(e.getMessage(),e);
		result.put("result", "0");
		//result.put("msg", "【" + e.getMessage() + "】");
		result.put("msg", "【" + e.getMessage() + "】");
	}
	
	// AJAX输出，返回null
		public String ajax(String content, String type, HttpServletResponse response) {
			try {
				response.setContentType(type + ";charset=UTF-8");
				response.setHeader("Pragma", "No-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
				response.getWriter().write(content);
				response.getWriter().flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		// text输出，返回null
	    public void returnTextString(HttpServletResponse response, String content) {
	        try {
	        	response.setContentType("text/plain;charset=UTF-8");
	    		response.setCharacterEncoding("UTF-8"); 
	    		PrintWriter out=response.getWriter();	     		
	    		out.print(content);
	    		out.flush();
	    		out.close();
	        }
	        catch (Exception e) {
	        	log.error("",e);
	            e.printStackTrace();
	        }      
	    }
	    
	    // json输出，返回null
	    public void returnJosnString(HttpServletResponse response, String content) {
	        try {
	        	response.setContentType("application/json;charset=UTF-8");
	    		response.setCharacterEncoding("UTF-8"); 
	    		PrintWriter out=response.getWriter();	     		
	    		out.print(content);
	    		out.flush();
	    		out.close();
	        }
	        catch (Exception e) {
	        	log.error("",e);
	            e.printStackTrace();
	        }      
	    }
}
